package com.qdlc.p2p.biz.processor.task;

import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qdlc.p2p.biz.processor.event.BorrowValueEvent;
import com.qdlc.p2p.biz.processor.task.collection.TenderTaskProcessor;
import com.qdlc.p2p.common.constant.TradeCode;
import com.qdlc.p2p.common.core.queue.QueueHandlers;
import com.qdlc.p2p.common.util.NumberUtil;
import com.qdlc.p2p.dal.dto.TppTradeLog;
import com.qdlc.p2p.dal.model.BorrowModel;

@Service
@Transactional
public class CollectionTradeNotifyTenderTaskProcessor extends AbstractCollectionTradeNotifyTendeTaskProcessor {
	
	/**
	 * 支持的外部外部业务码
	 * @return
	 */
	public TradeCode tradeCode(){
		return TradeCode.C1001;
	}
	
	/**
	 * 代收成功后需要处理的逻辑
	 */
	protected void doPay(TppTradeLog tppTradeLog, BorrowModel bm){
		JSONObject merPivJson = JSON.parseObject(tppTradeLog.getMemo());
		long rateInterestId = NumberUtil.getLong(merPivJson.getString("rateId"));
		String redEnvelopeStr = merPivJson.getString("redStr");
		double redEnvelopeMoney = merPivJson.getDoubleValue("redMoney");
		String experStr = merPivJson.getString("experStr");
		double experienceMoney = merPivJson.getDoubleValue("experMoney");
		long userId = NumberUtil.getLong(merPivJson.getString("userId"));
		bm.setUserId(userId);
		bm.setRateInterestId(rateInterestId);
		bm.setRedEnvelopeStr(redEnvelopeStr);
		bm.setTotalRedEnvelopeMoney(redEnvelopeMoney);
		bm.setExperiences((long[]) ConvertUtils.convert(experStr.split(","), long.class));
		bm.setTotalExperienceMoney(experienceMoney);
		BorrowValueEvent borrowEvent = new BorrowValueEvent();
		borrowEvent.setBorrowModel(bm);
		borrowEvent.setTaskProcessorClass(TenderTaskProcessor.class);
		QueueHandlers.put(borrowEvent);
	}
}
